#!/bin/bash
#
# Launcher for memprof parser.
# Copyright (C) 2015-2019 IPONWEB Ltd. See Copyright Notice in COPYRIGHT

UJIT_PREFIX=/usr
if [[ `which pkg-config` != "" ]]; then
    # Assuming that uJIT is installed in the system, try to guess its
    # installation prefix (/usr, /usr/local, /opt, etc.) with the help of
    # pkg-config.
    PREFIX=$(pkg-config --variable=prefix ujit)
    if [[ "$PREFIX" != "" ]]; then
        UJIT_PREFIX=$PREFIX
    fi
fi

UJIT_BIN=$UJIT_PREFIX/bin/ujit
UJIT_TOOLS_PREFIX=$UJIT_PREFIX/share/ujit

LAUNCHER_DIR=$(dirname `readlink -f $0`)
if ! [[ "$LAUNCHER_DIR" =~ "$UJIT_PREFIX" ]]; then
    # If we are launched from the source tree, override prefixes.
    UJIT_BIN=$LAUNCHER_DIR/../src/ujit
    UJIT_TOOLS_PREFIX=$LAUNCHER_DIR

    if [[ ! -x "$UJIT_BIN" ]]; then
        echo "FATAL: Unable to find uJIT at $UJIT_BIN. Is it built?"
        exit 1
    fi
fi

TOOL_DIR=$UJIT_TOOLS_PREFIX/parse_memprof

LUA_PATH="$TOOL_DIR/?.lua;;" $UJIT_BIN $TOOL_DIR/main.lua $@
